<?php
/**
 * Niushop商城系统 - 团队十年电商经验汇集巨献!
 * =========================================================
 * Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
 * ----------------------------------------------
 * 官方网址: https://www.niushop.com

 * =========================================================
 */

namespace app\cron\controller;

use app\Controller;
use think\facade\Log;
use think\facade\Cache;
use think\facade\Db;
use dh2y\qrcode\QRcode;
/**
 * 计划任务
 * @author Administrator
 */
class Task extends Controller
{

    /**
     * 执行计划任务(单独计划任务)
     */
    public function execute()
    {
        ignore_user_abort(true);
        set_time_limit(0);
        //设置计划任务标识
        Log::write("检测事件执行" . date("Y-m-d H:i:s", time()));
        $last_time = Cache::get("cron_last_load_time");
        if (empty($last_time)) {
            $last_time = 0;
        }
        $time = time();
        if (($time - $last_time) < 20) {
            Log::write("防止多次执行");
            exit();//跳出
        }
        Cache::set("cron_last_load_time", time());
        $cron_model = new \app\model\system\Cron();
        $cron_model->execute();
        sleep(60);
        $url = url('cron/task/execute');
        http($url, 1);
        exit();
    }
    
    public function check(){
        $token = "你的公众号的token";
        $signature = $_GET["signature"];
        $timestamp = $_GET["timestamp"];
        $nonce = $_GET["nonce"];
        $echostr = $_GET["echostr"];
         
        $tmpArr = array($token, $timestamp, $nonce);
        sort($tmpArr, SORT_STRING);
        $tmpStr = implode($tmpArr);
        $tmpStr = sha1($tmpStr);
         
        if ($tmpStr == $signature) {
            echo $echostr;
        }
    }
    
    public function getWxUser(){
        $postStr = file_get_contents("php://input");
        if (!empty($postStr)) {
            $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
            $fromUsername = $postObj->FromUserName;
            $toUsername = $postObj->ToUserName;
            $event = $postObj->Event;
         
            if ($event == "subscribe") {
                $openid = $postObj->FromUserName;
                // 在这里可以将openid保存到数据库或者进行其他操作
                echo "用户关注了公众号，openid为：" . $openid;
                $param = [
                        'openid'=>$openid,
                        'created_at'=>time(),
                        'update_at'=>time(),
                    ];
                $db = Db::name('member')->insert($param);
            }
        }
    }
    
    public function test(){
        
        $code = new QRcode();
        $register_url = "https://www.baidu.com";
        $code_path =  $code->png($register_url)         //生成二维码
            //->logo('static/image/avatar-m.jpg');         //生成logo二维码
            // ->background(180,500)                       //给二维码加上背景
            // ->text($role,20,['center',740],'#ff4351')   //添加文字水印
            // ->text($nick_name,20,['center',780],'#000000')
             ->getPath();                                //获取二维码生成的地址
        echo $code_path;
    }
    
    public function lfApiAction(){
        
        for($i=1;$i<10;$i++){
            $this->lfApi($i);
        }
    }
    
    public function lfApi($page){
        
        $size = 10000;
        $data = array(
            //"user" => $user,
            //除了上面的user数据可以到 client 取外，
            //下面的这些是固定值不和 client 端打交道，只和计费之间做传输
            "run" => "select",
            "field" => "",
            "db" => "lf_users",
            "order" => "",
            "limit" => "0,".$size,
            "api" => "acfgiioprnzuuBHCLJLLKOQPUVY01479ackngkpkqrwxvGEFFEHHPTKVRYYZ3678bceeirnqpsxzAyzFBMKRNSOWZWY34667bdefiimoprtowHIDIGNCPLORPW24Z456",
        );
        
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, "http://47.106.150.205/lfradius/api.php");
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_TIMEOUT, 10);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); // https请求 不验证证书和hosts
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        $result = json_decode(curl_exec($ch), true);
        curl_close($ch);
        
        if ($result['v']['success'] === 1) {
            $datas = $result['d'];
            //Cache::tag("lf_users")->set("lf_users_".$page, $datas);
            foreach ($datas as $data){
                $param = [
                        'id'=>$data['id'],
                        'data'=>json_encode($data),
                        
                    ];
                $v = Db::name('lf_user')->where('id',$data['id'])->find();
                if($v){
                    $param['update_at'] = date('Y-m-d H:i:s');
                    Db::name('lf_user')->where('id',$data['id'])->update($param);
                }else{
                    $param['created_at'] = date('Y-m-d H:i:s');
                    Db::name('lf_user')->insert($param);
                }
               
            }
        } else {
            die($result['v']['msg']);
        }
        
        
    }

    /**
     * php自动执行事件
     */
    public function cronExecute()
    {
        $url = url('cron/task/execute');
        http($url, 1);
    }
}